package com.guozinhs.auto.frame.listener;

import com.guozinhs.auto.frame.ActType;
import lombok.AllArgsConstructor;

import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

/**
 * 键盘按键监听器
 * <p>
 * 1. 保存项目
 *
 * @Author: GuoZi
 * @Date: 2022/3/28 16:50
 */
@AllArgsConstructor
public class FrameKeyListener implements AWTEventListener {

    ActionListener actionListener;

    public static final char KEY_SAVE = 19;

    @Override
    public void eventDispatched(AWTEvent event) {
        KeyEvent keyEvent = (KeyEvent) event;
        if (keyEvent.getID() != KeyEvent.KEY_TYPED) {
            return;
        }
        char keyChar = keyEvent.getKeyChar();
        if (KEY_SAVE == keyChar && keyEvent.isControlDown()) {
            ActionEvent actionEvent = new ActionEvent(event.getSource(), event.getID(), ActType.updateState.name());
            actionListener.actionPerformed(actionEvent);
        }
    }

}
